package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.pojo.Book;
import com.chengwang.bookstory.pojo.BookUpload;
import com.chengwang.bookstory.pojo.Manager;
import com.chengwang.bookstory.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("book")
public class bookController {
    @Autowired
    private BookService bookService;

    // 根据图书分类查询
    @RequestMapping("/queryByType")
    public ModelAndView queryByType(HttpServletRequest request,HttpSession session){
        ModelAndView mv=new ModelAndView();
        String bookType = request.getParameter("bookType");
        String searchMessage = (String) session.getAttribute("searchMessage");
        if (searchMessage != null && !searchMessage.isEmpty() ) {
            session.removeAttribute("searchMessage");
        }
        session.setAttribute("searchMessage", bookType);
        System.out.println(bookType);
        List<Book> bookList=bookService.queryByBookType(bookType);
        mv.addObject("bookList",bookList);
        mv.setViewName("search.html");
        return mv;
    }
    //根据名字查询
    @RequestMapping("/queryByBookName")
    public ModelAndView queryByBookName(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        String searchMessage = request.getParameter("searchMessage");
        HttpSession session = request.getSession();
        String searchMessage1 = (String) session.getAttribute("searchMessage");
        if (searchMessage != null && !searchMessage.isEmpty() ) {
            session.removeAttribute("searchMessage");
        }
        session.setAttribute("searchMessage", searchMessage);
        List<Book> bookList=bookService.queryByBookName(searchMessage);
        mv.addObject("bookList",bookList);
        mv.setViewName("search.html");
        return mv;
    }
    //管理端查询所有图书
    @RequestMapping("/queryAllBook")
    @ResponseBody
    public List<Book> queryAllBook(){
        List<Book> bookList=bookService.queryAllBook();
        return bookList;
    }
    //通过id查书
    @RequestMapping("/queryBookById")
    @ResponseBody
    public Book queryBookById(String bookId){
       Book book=bookService.queryBookById(bookId);
       return book;
    }
    //通过id跳转网页
    @RequestMapping("/queryBookByIdOne")
    public ModelAndView queryBookByIdOne(String bookId){
        ModelAndView mv = new ModelAndView();
        Book book=bookService.queryBookById(bookId);
        List<Book> bookList=new ArrayList<>();
        bookList.add(book);
        mv.addObject("bookList",bookList);
        mv.setViewName("search.html");
        return mv;
    }

    //修改图书
    @RequestMapping("/updateBook")
    public ModelAndView updateBook(Book book,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        bookService.updateBook(book);
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        mv.addObject("manager",manager);
        mv.setViewName("manager.html");
        return mv;
    }

    //添加图书
    @RequestMapping("/upload")
    public ModelAndView upload(BookUpload bookUpload,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        bookService.upload(bookUpload,request);
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        mv.addObject("manager",manager);
        mv.addObject("mes","增添成功");
        mv.setViewName("manager.html");
        return mv;
    }
    //按照名字查询图书
    @RequestMapping("/queryBookByName")
    public ModelAndView queryBookByName(String bookName,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        mv.addObject("manager",manager);
        List<Book> bookList=bookService.queryBookByBookName(bookName);
        if (bookList.size()>0){
        mv.addObject("queryBook",bookList);
        }else {
            mv.addObject("mes","查无此书");
        }
        mv.setViewName("manager.html");
        return mv;
    }

}
